package com.schoolai.schoolset.controller;

import com.schoolai.entity.SchoolSetNews;
import com.schoolai.schoolset.req.SetNewsREQ;
import com.schoolai.schoolset.service.ISchoolSetNewsService;
import com.schoolai.util.base.Result;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.Date;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author bobby
 * @since 2025-10-30
 */
@RestController
@Slf4j
@Tag(name = "新闻", description = "新闻相关操作")
@RequestMapping("/news")
public class SchoolSetNewsController {
    @Autowired
    ISchoolSetNewsService schoolSetNewsService;
    @Operation(summary = "查询新闻")
    @PostMapping("/search")
    @ApiResponses({
            @ApiResponse(responseCode = "20000", description = "操作成功"),
            @ApiResponse(responseCode = "999", description = "错误")
    })
    Result search(@RequestBody SetNewsREQ req) {
        return Result.ok(schoolSetNewsService.queryPage(req));
    }
    @Operation(summary = "通过id获取新闻")
    @GetMapping("/{id}")
    @ApiResponses({
            @ApiResponse(responseCode = "20000", description = "操作成功"),
            @ApiResponse(responseCode = "999", description = "错误")
    })
    @Cacheable(cacheNames = "news",key="#root.targetClass.name + '.' +#id")
    public Result view(@PathVariable("id") String id) {
        return Result.ok(schoolSetNewsService.getById(id));
    }

    @Operation(summary = "新增新闻")
    @PostMapping
    @ApiResponses({
            @ApiResponse(responseCode = "20000", description = "操作成功"),
            @ApiResponse(responseCode = "999", description = "错误")
    })
    public Result save(@RequestBody SchoolSetNews schoolSetNews){
        schoolSetNews.setCreatedTime(new Date());
        return Result.ok(schoolSetNewsService.saveOrUpdate(schoolSetNews));
    }
    @Operation(summary = "修改新闻")
    @PutMapping
    @ApiResponses({
            @ApiResponse(responseCode = "20000", description = "操作成功"),
            @ApiResponse(responseCode = "999", description = "错误")
    })
    // 更新或删除时清空缓存
    @CacheEvict(cacheNames = "news",key="#root.targetClass.name + '.' +#schoolSetNews.id")
    public Result update(@RequestBody SchoolSetNews schoolSetNews){
        schoolSetNews.setUpdatedTime(new Date());
        return Result.ok(schoolSetNewsService.saveOrUpdate(schoolSetNews));
    }

    @Operation(summary = "通过id下降新闻")
    @DeleteMapping("/{id}")
    @ApiResponses({
            @ApiResponse(responseCode = "20000", description = "操作成功"),
            @ApiResponse(responseCode = "999", description = "错误")
    })
    // 更新或删除时清空缓存
    @CacheEvict(cacheNames = "news",key="#root.targetClass.name + '.' +#id")
    public Result delete(@PathVariable("id") String id) {
        return schoolSetNewsService.delete(id);
    }
}
